﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HostAssistant
{
    public partial class updatepwdms : U1Form
    {
        string id = "";
        string name = "";
        string dbname = "";
        string type = "";
        public updatepwdms(string _id)
        {
            id = _id;
            InitializeComponent();
        }
        private void addsites_Load(object sender, EventArgs e)
        {
            var SqlInfo = dbbll.GetSqlInfo(id);
            name = SqlInfo?.Mdbname ?? "";
            dbname = SqlInfo?.Mdbname ?? "";
            type = SqlInfo?.type ?? "";

            

            lblname.Text = SqlInfo?.Mdbname ?? "";
           // lbltype.Text = SqlInfo?.type ?? "";
          //  lblpath.Text = SqlInfo?.Mpath ?? "";
          //  lbltime.Text = SqlInfo?.Mtime.ToString("yyyy-MM-dd HH:mm:ss") ?? "";
        }

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 重置密码

        private void simpleButton3_Click(object sender, EventArgs e)
        {

         
           

            if (String.IsNullOrWhiteSpace(txtpwd.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Regex r = new Regex(@"[a-zA-Z](?=.*?[1-9].*?)[a-zA-Z1-9]+|[1-9](?=.*?[a-zA-Z].*?)[a-zA-Z1-9]+");
            if (!r.IsMatch(txtpwd.Text.Trim()))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Regex r2 = new Regex(@"((?=[\x21-\x7e]+)[^A-Za-z0-9])");
            if (!r2.IsMatch(txtpwd.Text.Trim()))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtpwd.Text.Trim().Length < 8)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            try
            {


                splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == type);
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";



            SqlDbHelper.Initialize("master", user, password);
                if (SqlDbHelper.ConnectionTest())
                {
                    string newpassword = txtpwd.Text.Trim();

                    if (SqlDbHelper.ExecuteNormalSql(" EXECUTE sp_password NULL,'" + newpassword + "','" + dbname + "'"))
                    {

                        SqlInfo mysqlinfo = new SqlInfo();
                        mysqlinfo.ID = ZConvert.StrToInt(id);
                        mysqlinfo.Mpass = newpassword;
                        string query = "update SqlInfo set Mpass=@Mpass  where  id=@ID   ";
                        Data<SqlInfo>.GetInstance().Update(query, mysqlinfo);
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("成功修改密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();

                }
                    else
                    {
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("密码修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库连接失败请检查配置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        #endregion

        #region 备份数据库

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if(type=="mysql")
            {
                mysqlbak f2 = new mysqlbak(id);
                f2.ShowDialog(this);
            }
            else
            {
                mssqlbak f2 = new mssqlbak(id);
                f2.ShowDialog(this);
            }
         

            return;


            var p = PathSetbll.GetPathSet();


                  if (p==null || string.IsNullOrWhiteSpace( p?.DbBakPath)  )
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("未设置备份路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }




            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mysql");
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";
            MySqlDbHelper mdb = new MySqlDbHelper();
            mdb.Initialize(ip, "mysql", user, password, port);
            if (mdb.ConnectionTest() == null)
            {
               string s=   dbbll.EcreateTaskSchd_Mysql_Single( "", dbname, p.DbBakPath+ @"\mysql\"+ dbname + "\\");
             
                if(s!=null)
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show(s, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("备份完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

               
            }
            else
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("未能连接上数据库,请检查数据库账号密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region 还原数据库
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (type == "mysql")
            {
                mysqlrockbak f2 = new mysqlrockbak(id);
                f2.ShowDialog(this);
            }
            else
            {
                mssqlrockbak f2 = new mssqlrockbak(id);
                f2.ShowDialog(this);

            }

            return;
        }
        #endregion
    }
}